#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Bootloader_TesterPresent_0x3E_test.py
@Time    :   2024/09/11 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume

import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

TesterPresent_NRC = [([0x3E], [0x7F, 0x3E, 0x13]),
                     ([0x3E, 0x00, 0x01], [0x7F, 0x3E, 0x13]),
                     ([0x3E, 0x05, 0x01], [0x7F, 0x3E, 0x13]),
                     ([0x3E, 0x01], [0x7F, 0x3E, 0x12]),
                     ([0x3E, 0x20], [0x7F, 0x3E, 0x12]),
                     ([0x3E, 0x7F], [0x7F, 0x3E, 0x12])]


TesterPresent_SuppressPosMsg_NRC = [([0x3E, 0x80, 0x01], [0x7F, 0x3E, 0x13]),
                                    ([0x3E, 0x85, 0x01], [0x7F, 0x3E, 0x13]),
                                    ([0x3E, 0x81], [0x7F, 0x3E, 0x12]),
                                    ([0x3E, 0xA0], [0x7F, 0x3E, 0x12]),
                                    ([0x3E, 0xFF], [0x7F, 0x3E, 0x12])]


class Test_TesterPresent_Phy(object):
    @allure.severity('normal')
    @allure.story('Tester Present Test')
    @allure.description('Test positive response of 0x3E in programming session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 2, iStep, False)


    @allure.severity('normal')
    @allure.description('Test suppressPosMsgBit is enabled 0x3E in programming session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_SuppressPosMsgBit_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 2, iStep, False)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_NRC]
    @allure.severity('minor')
    @allure.description('Test negative response code of 0x3E when suppressPosMsgBit is enabled in bootloader')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC, ids=ids)
    def test_TesterPresent_NRC_Promming_phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)


    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_SuppressPosMsg_NRC]
    @allure.severity('minor')
    @allure.description('Test negative response code of 0x3E when suppressPosMsgBit is enabled in bootloader')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_SuppressPosMsg_NRC, ids=ids)
    def test_TesterPresent_SuppressPosMsg_NRC_Promming_phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)


class Test_TesterPresent_Fun(object):
    @allure.severity('normal')
    @allure.story('Tester Present Test')
    @allure.description('Test positive response of 0x3E in programming session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_Fun(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 2, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.description('Test suppressPosMsgBit is enabled 0x3E in programming session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_SuppressPosMsgBit_Fun(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 2, iStep, False, 'UDSFun')

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_NRC]
    @allure.severity('minor')
    @allure.description('Test negative response code of 0x3E in bootloader')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC, ids=ids)
    def test_TesterPresent_NRC_programming_Fun(self, action, Driver, req, resp):
        iStep = 1
        if resp[2] == 0x12:
            resp = []
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False, 'UDSFun')

    
    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_SuppressPosMsg_NRC]
    @allure.severity('minor')
    @allure.description('Test negative response code of 0x3E when suppressPosMsgBit is enabled in bootloader')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_SuppressPosMsg_NRC, ids=ids)
    def test_TesterPresent_SuppressPosMsg_NRC_Promming_fun(self, action, Driver, req, resp):
        iStep = 1
        if resp[2] == 0x12:
            resp = []
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False, 'UDSFun')